using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using 餐饮_企划.Models;

namespace 餐饮_企划
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            //services.AddSingleton<RecipeServices>();    //将RecipeSercice对象放到对象池中，对象的生命周期：全局  ==》 应用程序开始 到 应用程序结束
            services.AddTransient<RecipeDB>();    //将RecipeDB对象放到对象池中，对象的生命周期：Transient  ==》 应用程序开始 到 应用程序结束
            services.AddTransient<UserDB>();


            string ConnStr = "server=.;database=Meun;uid=sa; pwd=123456";
            services.AddDbContext<MyDbContext>(option => option.UseSqlServer(ConnStr));  //对象的生命周期：Singleton


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // 异常中间件
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            

            app.UseHttpsRedirection();

            app.UseDefaultFiles(); // 使用默认文件中间件，index.html,index.htm, default.html ,default.htm

            
            app.UseStaticFiles(); // 添加静态文件中间件   使用wwwroot下的静态文件



            app.UseRouting();

            app.UseAuthorization();
            
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Menu}/{id?}");
            });
        }
    }
}
